package springdao.model;

import java.util.Locale;
import java.util.Properties;
import java.util.StringTokenizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.i18n.LocaleContextHolder;
import springdao.support.StringEnumUserType;

/**
 *
 * @author Kent Yeh
 */
public class SupplyChainMemberType extends StringEnumUserType<SupplyChainMember> {
    
    private static Logger logger = LoggerFactory.getLogger(SupplyChainMemberType.class);
    
    @Override
    public void setParameterValues(Properties parameters) {
        if (parameters != null) {
            String localeValue = parameters.getProperty("locale");
            if (localeValue != null && !localeValue.isEmpty()) {
                String language = null, country = null, variant = null;
                StringTokenizer st = new StringTokenizer(localeValue, "_");
                while (st.hasMoreTokens()) {
                    String token = st.nextToken();
                    if (language == null) {
                        language = token;
                    } else if (country == null) {
                        country = token;
                    } else {
                        variant = token;
                    }
                }
                if (language != null && !language.isEmpty()) {
                    Locale locale = variant!=null?new Locale(language, country, variant):country!=null?new Locale(language, country):new Locale(language);
                    LocaleContextHolder.setLocale(locale);
                }
            }
        }
    }
    
    @Override
    protected String getOriginalValue(SupplyChainMember enumInstance) {
        if (SupplyChainMember.CUSTOMER.equals(enumInstance)) {
            return "C";
        } else if (SupplyChainMember.VENDOR.equals(enumInstance)) {
            return "V";
            
        } else if (SupplyChainMember.WAREHOUSE.equals(enumInstance)) {
            return "W";
        } else {
            return null;
        }
    }
    
    @Override
    protected SupplyChainMember getEnumInstance(String original) {
        if ("C".equals(original)) {
            return SupplyChainMember.CUSTOMER;
        } else if ("V".equals(original)) {
            return SupplyChainMember.VENDOR;
        } else if ("W".equals(original)) {
            return SupplyChainMember.WAREHOUSE;
        } else {
            return null;
        }
    }
    
    @Override
    public Class<SupplyChainMember> returnedClass() {
        return SupplyChainMember.class;
    }
}
